package com.angel.core.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * @author 作者姓名 cheng
 * @version 创建时间：2016年10月11日 下午3:04:01 类说明
 */
public class DateUtils {

	/**
	 * 日期
	 * @return
	 */
	public static String getNowForStr() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = new Date();
		String now = "";
		now = sdf.format(date);
		return now;
	}
	
	/**
	 * 时间
	 * @return
	 */
	public static String getNowTimeForStr() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = new Date();
		String now = "";
		now = sdf.format(date);
		return now;
	}

	/**
	 * 获取年月日
	 * 
	 * @param date
	 * @return
	 */
	public static Date getDate(String dateStr) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		Date date = null;
		try {
			date = sdf.parse(dateStr);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			return null;
		}
		return date;
	}

	/**
	 * 获取年月日时分秒
	 * 
	 * @param date
	 * @return
	 */
	public static Date getDateTime(String dateStr) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

		Date date = null;
		try {
			date = sdf.parse(dateStr);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			return null;
		}
		return date;
	}

	/**
	 * 日期下一天
	 * 
	 * @param now
	 * @return
	 */
	public static String getNextDay(String now) {
		Date nowdate;
		String nday = new String();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		try {
			nowdate = sdf.parse(now);
			long oneday = 1000 * 3600 * 24;
			Date nextday = new Date(nowdate.getTime() + oneday);
			nday = sdf.format(nextday.getTime());

		} catch (Exception e) {
			e.printStackTrace();
		}
		return nday;
	}

	/**
	 * 日期下一月
	 * 
	 * @param now
	 * @return
	 */
	public static String getNextMonth(String now) {
		Date t;
		String nmonth = new String();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		try {
			t = sdf.parse(now);
			Date nextmonth = new Date((t.getYear() + (t.getMonth() + 1) / 12), (t.getMonth() + 1) % 12, t.getDate());
			nmonth = sdf.format(nextmonth.getTime());

		} catch (Exception e) {

			e.printStackTrace();
		}
		return nmonth;
	}

	/**
	 * 给日期加天数
	 * 
	 * @param num
	 * @param newDate
	 * @return
	 * @throws ParseException
	 */
	public static String plusDay(int num, String newDate) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Date currdate;
		String enddate = "";
		try {
			currdate = format.parse(newDate);
			Calendar ca = Calendar.getInstance();
			ca.add(Calendar.DATE, num);// num为增加的天数，可以改变的
			currdate = ca.getTime();
			enddate= format.format(currdate);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return enddate;
	}
}
